import axios from "axios";
import store from "../store";
import router from "../router";
import Vue from "vue";
import { Notification, Loading } from "element-ui";
const option = {
    text: "数据加载中,请稍后!",
    background: "rgba(0, 0, 0, 0.6)"
};
const errorHandle = (status, other) => {
    Notification({
        type: "error",
        message: other,
        duration: 2000,
        position: "bottom-right"
    });
};
function getCheckCode(checkIndex, userId) {
    var charArr = checkIndex.split("");
    var checkCode = "";
    for (var i = 0; i < charArr.length; i++) {
        checkCode += userId.charCodeAt(parseInt(charArr[i]));
    }
    return checkCode;
}
export function fetch(options) {
    return new Promise((resolve, reject) => {
        const instance = axios.create({
            // 创建axios实例
            headers: {
                "Content-Type": "text/plain; charset=utf-8",
                "Access-Control-Allow-Origin": "*"
            },
            withCredentials: true, // 跨域带上cookies
            timeout: 60000
        });
        instance.interceptors.request.use(
            config => {
                // 请求拦截器
                store.commit("AxiosJia");
                Loading.service(option);

                if (router.history.current.path !== "/login") {
                    let userId = JSON.parse(sessionStorage.getItem("userInfo"));
                    let getCheck = "";
                    if (userId !== null) {
                        getCheck = getCheckCode(
                            sessionStorage.getItem("checkIndex"),
                            userId.id
                        );
                    }
                    let vues = new Vue();
                    if (sessionStorage.getItem("token") !== null) {
                        config.headers.Authorization = sessionStorage.getItem("token");
                        config.headers.user_type = 2;
                        config.headers.checkCode = vues.utility.jiami(getCheck);
                    } else {
                        store.state.AxiosNull = 0;
                        Loading.service(option).close();
                        errorHandle(400, "登录已过期！重新登录");
                        router.push("/login");
                    }
                }
                return config;
            },
            error => {
                store.state.AxiosNull = 0;
                Loading.service(option).close();
                return Promise.reject(error);
            }
        );
        instance.interceptors.response.use(
            data => {
                // 响应拦截器
                Loading.service(option).close();
                store.commit("AxiosJian");
                if (store.state.AxiosNull === 0) {
                    Loading.service(option).close();
                }
                if (data.data.code === "401") {
                    errorHandle(400, "登录已过期！重新登录");
                    sessionStorage.removeItem("userInfo");
                    sessionStorage.removeItem("token");
                    sessionStorage.removeItem("checkIndex");
                    router.push("/login");
                }
                return Promise.resolve(data); // 返回数据
            },
            function (error) {
                errorHandle(400, "连接超时");
                store.state.AxiosNull = 0;
                store.commit("AxiosAbnormal");
                Loading.service(option).close();

                return Promise.reject(error);
            }
        );
        // 请求处理
        if (options.method === "get") {
            instance
                .get(options.url, { params: options.data })
                .then(function (response) {
                    resolve(response);
                })
                .catch(function (error) {
                    reject(error);
                });
        } else {
            instance(options)
                .then(response => {
                    resolve(response);
                })
                .catch(error => {
                    reject(error);
                });
        }
    });
}
